package com.woniu.service.impl;

import com.woniu.entity.RbacManager;
import com.woniu.entity.RbacMenu;
import com.woniu.entity.ResponseEntity;
import com.woniu.mapper.RbacManagerMapper;
import com.woniu.mapper.RbacMenuMapper;
import com.woniu.mapper.RbacRoleMapper;
import com.woniu.service.RbacMenuService;
import com.woniu.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/09 11:14
 */
@Service
public class RbacMenuServiceImpl implements RbacMenuService {

    @Autowired
    RbacMenuMapper menuMapper;

    @Autowired
    RbacManagerMapper rbacManagerMapper;

    @Autowired
    RbacRoleMapper rbacRoleMapper;

    @Override
    public ResponseEntity findAllMenu() {
        List<RbacMenu> rbacMenus = menuMapper.findAll();
        return new ResponseEntity(rbacMenus);
    }

    @Override
    public ResponseEntity editOne(RbacMenu menu) {
        int i =menuMapper.editOne(menu);
        if (i > 0)
            return new ResponseEntity(200, "修改成功", null);
        else
            return new ResponseEntity(500, "修改失败", null);

    }

    @Override
    public ResponseEntity updateOneMenuFID(Integer id, Integer fid) {
        int i = menuMapper.updateOneMenuFID(id, fid);
        if (i > 0)
            return new ResponseEntity(200, "修改成功", null);
        else
            return new ResponseEntity(500, "修改失败", null);
    }

    @Override
    public ResponseEntity findMenuByManagerID(Integer id) {
        //查出用户的角色
        RbacManager oneById = rbacManagerMapper.findOneById(id);
        //查出角色所拥有的ID
        List<Integer> menuIDs = rbacRoleMapper.findMenuIDByRoleID(oneById.getRoleId());
        List<RbacMenu> menus = menuMapper.findAll();
        //通过ID过滤所有的menu
        MyUtils.filterMenus(menus, menuIDs);

        return new ResponseEntity(201,"OK",menus);
    }
}
